#!/bin/bash
show_menu(){
    NORMAL=`echo "\033[m"`
    MENU=`echo "\033[36m"`
    NUMBER=`echo "\033[33m"`
    FGRED=`echo "\033[41m"`
    RED_TEXT=`echo "\033[31m"`
    ENTER_LINE=`echo "\033[33m"`
    echo -e "${MENU}******************************************${NORMAL}"
    echo -e "${MENU}**${NUMBER} 1)${MENU} 构建镜像并启动容器                **${NORMAL}"
    echo -e "${MENU}**${NUMBER} 2)${MENU} 下载镜像并启动容器                **${NORMAL}"
    echo -e "${MENU}**${NUMBER} 3)${MENU} 重启容器                          **${NORMAL}"
    echo -e "${MENU}**${NUMBER} 4)${MENU} 查看sync容器日志                  **${NORMAL}"
    echo -e "${MENU}**${NUMBER} 5)${MENU} 查看nginx容器日志                 **${NORMAL}"
    echo -e "${MENU}******************************************${NORMAL}"
    echo -e "${ENTER_LINE}请输入你要进行的操作的序号并回车${RED_TEXT}或直接按下回车退出 ${NORMAL}"
    read opt
}
function option_picked() {
    COLOR='\033[01;31m'
    RESET='\033[00;00m'
    MESSAGE=${@:-"${RESET}Error: No message passed"}
    echo -e "${COLOR}${MESSAGE}${RESET}"
}
if [[ ! -d "./data" ]]; then
    mkdir -p ./data
fi
if [[ ! -d "./log" ]]; then
    mkdir -p ./log
fi
if [[ ! -d "./server" ]]; then
    mkdir -p ./server
fi
if [[ ! -d "./nginx/logs" ]]; then
    mkdir -p ./nginx/logs
fi
if [[ ! -d "./nginx/ssl/cert.crt" ]]; then
    touch ./nginx/ssl/cert.crt
fi
if [[ ! -d "./nginx/ssl/cert.key" ]]; then
    touch ./nginx/ssl/cert.key
fi
echo -e "\n ================ \n ${Info} ${GreenBG} 即将检查此项目更新 ${Font} \n ================ \n"
git pull
clear
show_menu
while [ opt != '' ]
    do
    if [[ $opt = "" ]]; then 
            exit;
    else
        case $opt in
        1)clear;
            option_picked "docker-compose -f ./build.yaml up -d";
            docker-compose -f ./build.yaml up -d;
            show_menu;
        ;;
        2)clear;
            option_picked "docker-compose -f ./image.yaml up -d";
            docker-compose -f ./image.yaml up -d;
            show_menu;
            ;;
        3)clear;
            option_picked "docker restart lx-sync";
            docker restart lx-sync
            show_menu;
            ;;
        4)clear;
            option_picked "docker logs lx-sync";
            docker logs lx-sync
            show_menu;
            ;;
        5)clear;
            option_picked "docker logs lx-nginx";
            docker logs lx-nginx
            show_menu;
            ;;
        \n)exit;
            ;;
        *)clear;
            option_picked "请输入正确的选项";
            show_menu;
            ;;
    esac
fi
done